package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Name CartServiceImpl
 * @Author stella
 * @Date 2023-03-08 8:13 PM
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    /**
     * 购物车新增
     *
     * @param cartParam
     * @return
     */
    @Override
    public Cart add(Cart cartParam) {//insert into shoping_cart () valuse ()
        // 先查询购物车是否有数据
        //构建条件
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        // user id
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        // 套餐id  动态sql（前置条件！=null
        qw.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        // 菜品id  动态sql（前置条件！=null
        qw.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
        Cart cart = cartMapper.selectOne(qw);
        // 判断cart是否为空
        if (cart == null) {//如果为空 新增一条记录
            cartParam.setUserId(UserHolder.get().getId());
            cartParam.setNumber(1);
            cartParam.setCreateTime(new Date());
            cartMapper.insert(cartParam);
            cart = cartParam;
        } else { // 存在记录,购买数量+1
            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);
        }
        return cart;
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<Cart> list() {
        //查询购物车列表  select * from shopping_cart where user_id = ?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        List<Cart> cartList = cartMapper.selectList(qw);
        return cartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {//delete * from shopping_cart where user_id = ?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(qw);
    }

    /**
     * 购物车减少修改
     */
    @Override
    public Cart sub(Cart cart) {//动态sql   update  shopping_cart  ...set ... where  user_id = ?

        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        qw.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        Cart cart1 = cartMapper.selectOne(qw);
        if (cart1.getNumber() > 1) {// 购物车减去一
            cart1.setNumber(cart1.getNumber() - 1);
//            cart1.setSetmealId(cart.getSetmealId());
//            cart1.setDishId(cart.getDishId());
            cartMapper.updateById(cart1);

        }else{
            cart1.setNumber(0);
            cartMapper.updateById(cart1);
            cartMapper.delete(qw);

        }
        return cart1;
    }
}
